<script>
export default {
  name: 'ImprovementSuggestion',
  props: {
    pageData: {
      required: false,
      default: {}
    },
    queryParams: {
      required: false,
      default: {}
    },
    projectList: {
      required: false,
      default: []
    }
  },
  data() {
    return {
      project: {},
      content1: null,
      content2: null
    }
  },
  created() {
    this.$api.optimizationSuggestion.getContent({
      name: '碳中和总量为正值'
    }).then(response => {
      this.content1 = response.data
    })
    this.$api.optimizationSuggestion.getContent({
      name: '碳中和总量为负值'
    }).then(response => {
      this.content2 = response.data
    })
  },
  computed: {
    getProjectName() {
      let sceneProjectId = this.queryParams.sceneProjectId
      if (sceneProjectId == null && this.projectList.length > 0) {
        sceneProjectId = this.projectList[0].value
      }
      if (sceneProjectId != null) {
        this.project = this.pageData.projectTotalList.find(item => item.sceneProjectId === sceneProjectId) || {}
        if (this.project.sceneProjectId != null) {
          return this.projectList.find(project => project.value === this.project.sceneProjectId)?.label
        }
      }
      return null
    },
    getTotalActualCarbonNeutrality(){
      if(this.pageData.projectTotalList != null && this.pageData.projectTotalList.length > 0) {
        return this.pageData.projectTotalList.map(item => item.actualCarbonNeutrality)
          .reduce((accumulator, currentValue) => accumulator + currentValue, 0)
      }
      return 0;
    }
  },
  methods: {
    getMaxName(item) {
      let name = ''
      let deviation = 0
      let rate = 0
      if (item.carbonEmissionsDeviation < item.carbonZeroDeviation) {
        name = '碳减'
        deviation = item.carbonZeroDeviation
        rate = item.carbonZeroDeviationRate
      } else {
        name = '碳排'
        deviation = item.carbonEmissionsDeviation
        rate = item.carbonEmissionsDeviationRate
      }
      if (deviation < item.carbonSequestrationDeviation) {
        name = '碳汇'
        deviation = item.carbonSequestrationDeviation
        rate = item.carbonSequestrationDeviationRate
      }
      return {
        name,
        deviation,
        rate
      }
    },
    getMaxRowByProjectAndField(projectId, field) {
      const list = this.pageData.projectData[projectId] || []
      if (list.length === 0) {
        return {}
      }
      return list.filter(item => item[field] != null).sort((a, b) => b[field] - a[field])[0]
    },
    getMinRowByProjectAndField(projectId, field) {
      const list = this.pageData.projectData[projectId] || []
      if (list.length === 0) {
        return {}
      }
      return list.filter(item => item[field] != null).sort((a, b) => a[field] - b[field])[0]
    }
  }
}
</script>

<template>
  <div>
    <h2>6、优化建议</h2>
    <!--    <div v-for="project in pageData.projectTotalList" :key="project.sceneProjectId">-->
    <div style="margin:0 10%">
      <p>{{ getProjectName }}
        {{ pageData.startDate }}至{{ pageData.endDate }}，碳中和规实情况如下:
        {{ getProjectName }}规划碳中和总量为{{ project.planCarbonNeutrality }} kgCO2,
        实际碳中和总量为{{ project.actualCarbonNeutrality }}kgCO2，
        碳中和规实总偏差值为{{ project.carbonNeutralityDeviation }}kgCO2。
        其中，{{ getMaxRowByProjectAndField(project.sceneProjectId, 'carbonNeutralityDeviation').solidifyDate
        }}的碳中和规实偏差值最高，为{{ getMaxRowByProjectAndField(project.sceneProjectId, 'carbonNeutralityDeviation').carbonNeutralityDeviation
        }}kgCO2；
        {{ getMinRowByProjectAndField(project.sceneProjectId, 'carbonNeutralityDeviation').solidifyDate
        }}的碳中和规实偏差值最低，为{{ getMaxRowByProjectAndField(project.sceneProjectId, 'carbonNeutralityDeviation').carbonNeutralityDeviation
        }}kgCO2。</p>
    </div>
    <div style="padding-left: 5%">
      <p>具体分析及优化建议：</p>
      <p v-if="pageData.projectTotalList.length > 0">
        1、从碳中和规实偏差源情况分析，贡献最大的是{{ getMaxName(pageData.projectTotalList[0]).name }}，
        偏差值为{{ getMaxName(pageData.projectTotalList[0]).deviation }} CO2，偏差贡献率为
        {{ getMaxName(pageData.projectTotalList[0]).rate }}%，
        即表明{{ getMaxName(pageData.projectTotalList[0]).name }}对{{ getProjectName }}碳中和偏差情况影响程度最大。</p>
      <p>{{ project.carbonNeutralityDeviation >= 0 ? content1 : content2 }}</p>
      <p v-if="getTotalActualCarbonNeutrality > 0">2、从碳中和实际情况分析，碳中和总量为{{ getTotalActualCarbonNeutrality }}kgCO2，即表明碳排放量超过了通过光伏和碳汇所吸收的碳量。</p>
      <p v-if="getTotalActualCarbonNeutrality < 0">2、从碳中和实际情况分析，碳中和总量为 {{ getTotalActualCarbonNeutrality }} kgCO2，即表明光伏和碳汇产生的碳减和碳汇量超过了产生的碳排放量。</p>
    </div>
  </div>
</template>

<style scoped lang="scss">
p {
  text-indent: 2em;
  line-height: 24px;
}
</style>
